package co.yixiang.modules.activity.service.impl;

import co.yixiang.modules.activity.domain.YxStoreCoupon;
import co.yixiang.modules.activity.repository.YxStoreCouponRepository;
import co.yixiang.modules.activity.service.YxStoreCouponService;
import co.yixiang.modules.activity.service.dto.YxStoreCouponDTO;
import co.yixiang.modules.activity.service.dto.YxStoreCouponQueryCriteria;
import co.yixiang.modules.activity.service.mapper.YxStoreCouponMapper;
import co.yixiang.modules.shop.domain.YxStoreProduct;
import co.yixiang.modules.shop.repository.YxStoreProductRepository;
import co.yixiang.utils.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
* @author hupeng
* @date 2019-11-09
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreCouponServiceImpl implements YxStoreCouponService {

    @Autowired
    private YxStoreCouponRepository yxStoreCouponRepository;

    @Autowired
    private YxStoreCouponMapper yxStoreCouponMapper;

    @Autowired
    private YxStoreProductRepository productRepository;

    @Resource
    private RedisUtils redisUtils;


    @Override
    public Map<String,Object> queryAll(YxStoreCouponQueryCriteria criteria, Pageable pageable){
        Page<YxStoreCoupon> page = yxStoreCouponRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        List<JSONObject> coupons = new ArrayList<>();
        for (YxStoreCoupon yxStoreCoupon : page.getContent()) {
            JSONObject coupon = (JSONObject) JSONObject.toJSON(yxStoreCoupon);
            List<YxStoreProduct> productList = new ArrayList<>();
            coupon.put("productList",productList);
            if(yxStoreCoupon.getType() == 2 || yxStoreCoupon.getType() == 3 ){
                String productIds = yxStoreCoupon.getProductIds();
                if(StringUtils.isNotBlank(productIds)){
                    String[] ids = productIds.split(",");
                    List<Integer> intIds = new ArrayList<>();
                    for (String id : ids) {
                        intIds.add(Integer.valueOf(id));
                    }
                    productList = productRepository.findAllById(intIds);
                }
                coupon.put("productList",productList);
            }
            coupons.add(coupon);
        }
        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("content",coupons);
        map.put("totalElements",page.getTotalElements());
        return map;
    }

    @Override
    public List<YxStoreCouponDTO> queryAll(YxStoreCouponQueryCriteria criteria){
        return yxStoreCouponMapper.toDto(yxStoreCouponRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public YxStoreCouponDTO findById(Integer id) {
        Optional<YxStoreCoupon> yxStoreCoupon = yxStoreCouponRepository.findById(id);
        ValidationUtil.isNull(yxStoreCoupon,"YxStoreCoupon","id",id);
        return yxStoreCouponMapper.toDto(yxStoreCoupon.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public YxStoreCouponDTO create(YxStoreCoupon resources) {
        return yxStoreCouponMapper.toDto(yxStoreCouponRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(YxStoreCoupon resources) {
        Optional<YxStoreCoupon> optionalYxStoreCoupon = yxStoreCouponRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalYxStoreCoupon,"YxStoreCoupon","id",resources.getId());
        YxStoreCoupon yxStoreCoupon = optionalYxStoreCoupon.get();
        yxStoreCoupon.copy(resources);
        yxStoreCouponRepository.save(yxStoreCoupon);
        redisUtils.del("yxStoreCoupon:"+resources.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        yxStoreCouponRepository.deleteById(id);
    }
}